//
//  MainViewController.m
//  drawacartoon
//
//  Created by Ji Hu on 12-7-6.
//  Copyright (c) 2012年 爱辅导. All rights reserved.
//

#import "MainViewController.h"
#import "Log.h"
#import "ViewCartoonViewController.h"
#import "DragTableItemView.h"
#import "File.h"
#import "CDrawerView.h"
#import "PersistenceCenter.h"

@interface MainViewController ()

-(void)showViewControllerAtPopover:(UIViewController*)controller fromRect:(CGRect)rect;

@end

@implementation MainViewController

@synthesize dragTableView=_dragTableView;
@synthesize createButton=_createButton;
@synthesize popOver=_popOver;
@synthesize delegate=_delegate;

-(void)dealloc{
    self.dragTableView=nil;
    self.createButton=nil;
    self.popOver=nil;
    self.delegate=nil;
    [super dealloc];
}

-(void)didReceiveMemoryWarning{
    [Log warning:@"main controller receive memory warning!"];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        
        
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    //load cartoons
    NSString *cartoonpath = [File cartoonDirectoryPath];
    NSFileManager *fm = [NSFileManager defaultManager];
    if (cartoonpath!=nil) {
        NSArray *cartoons = [fm contentsOfDirectoryAtPath:cartoonpath error:nil];
        for (NSString *cartoon in cartoons) {
           [self.dragTableView addItemImage:[UIImage imageNamed:@"cat"] delegate:self withName:cartoon];
            
        }
    }
    
    CDrawerView *drawer = [[CDrawerView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:drawer];
    [drawer release];
    
    NSString *first = [[PersistenceCenter defaultCenter] objectForKey:@"first"];
    if (first==nil) {
         [[PersistenceCenter defaultCenter] setObject:@"yes" forKey:@"first"];
    }
   
    NSLog(@"first %@",first);
}

-(void)viewDidAppear:(BOOL)animated{
    [_dragTableView expandSubviews];
}

-(void)addOneCartoon:(id)sender{

    CreateCartoonViewController *createViewController = [[CreateCartoonViewController alloc] initWithNibName:@"CreateCartoonViewController" bundle:nil];
    createViewController.delegate = self;
    
    [self showViewControllerAtPopover:createViewController fromRect:self.createButton.frame];
    [createViewController release];
    
}

-(void)showViewControllerAtPopover:(UIViewController *)controller fromRect:(CGRect)rect{
    
    UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:controller];
    [popover setPopoverContentSize:controller.view.bounds.size];
    [popover presentPopoverFromRect:rect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    self.popOver = popover;
    [popover release];
    
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
	return YES;
}

-(void)operate:(int)op withObject:(id)object{
    
    if (op==Operate_OK) {
        [self.dragTableView addItemImage:[UIImage imageNamed:@"cat"] delegate:self withName:object];
    }
    
    if(self.popOver){
        [self.popOver dismissPopoverAnimated:YES];
        
    }
}

-(void)edit:(id)drawObject{
    
    [self.popOver dismissPopoverAnimated:YES];
    self.popOver=nil;
    
    [self.delegate openDrawCartoonViewController:nil];
}

-(void)itemClick:(DragTableItemView *)itemView{
   
    ViewCartoonViewController *viewCartoon = [[ViewCartoonViewController alloc] initWithName:itemView.name];
    viewCartoon.viewCartoonDelegate=self;
    [self showViewControllerAtPopover:viewCartoon fromRect:itemView.frame];
    
    [viewCartoon release];
}

@end
